package com.leve.admin.repository;

import com.leve.admin.domain.PushMessage;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class PushRepository {
    @Resource
    @Qualifier("mongoTemplate")
    private MongoOperations leveMongoOperations;


    public void addPush(PushMessage pushMessage) {
        leveMongoOperations.insert(pushMessage, "pushMessage");
    }

    public List<PushMessage> pushs(PageRequest pageRequest) {
        Query query = Query.query(Criteria.where("isDelete").is(false));
        return leveMongoOperations.find(query.with(new Sort(Sort.Direction.DESC, "createdDate")).with(pageRequest), PushMessage.class, "pushMessage");

    }
}
